import { Column, CreateDateColumn, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn, Unique, UpdateDateColumn } from 'typeorm'
import { Exclude } from 'class-transformer'
import { Role } from '../../role/entities/role.entity'

@Entity({
  name: 'users',
})
export class User {
  @PrimaryGeneratedColumn()
  id: number

  @Column({
    length: 50,
    comment: '用户名',
    unique: true,
  })
  username: string

  @Column({
    length: 100,
    comment: '密码',
  })
  @Exclude()
  password: string

  @Column({
    name: 'nick_name',
    length: 50,
    comment: '昵称',
  })
  nickName: string

  @Column({
    comment: '邮箱',
    length: 50,
  })
  email: string

  @Column({
    comment: '头像',
    length: 100,
    nullable: true,
  })
  headPic: string

  @Column({
    comment: '手机号',
    length: 20,
    nullable: true,
  })
  phoneNumber: string

  @Column({
    comment: '是否冻结',
    default: false,
  })
  isFrozen: boolean

  @Column({
    comment: '是否是管理员',
    default: false,
  })
  isAdmin: boolean

  @CreateDateColumn()
  createTime: Date

  @UpdateDateColumn()
  updateTime: Date

  @Column({
    type: 'int',
    comment: '登录类型, 0 用户名密码登录, 1 Google 登录, 2 Github 登录',
    default: 0,
  })
  loginType: LoginType

  @ManyToMany(() => Role)
  @JoinTable({
    name: 'user_roles',
  })
  roles: Role[]
}

export enum LoginType {
  USERNAME_PASSWORD = 0,
  GOOGLE = 1,
  GITHUB = 2,
}
